package com.recyclerview422zfj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView;

import com.recyclerview422zfj.R;
import com.recyclerview422zfj.bean.VideoBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/5/26.
 */

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {
    List<VideoBean> list;
    Context context;
    OnItemClickListener mOnItemClickListener;
    public VideoAdapter(Context context,List<VideoBean> list ) {
        this.context=context;
        this.list=list;
    }
    public void setList(List<VideoBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        ViewHolder holder=new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int mposition=position;
        VideoBean bean=list.get(position);
        holder.textView.setText(bean.getName());
        holder.textView2.setText(bean.getDescription());
        holder.textView3.setText(bean.getUpdate_time());
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+ bean.getThumb())
                .placeholder(R.drawable.f1)
                .into(holder.imageView);
        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(view,mposition);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView,textView2,textView3;
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.textView);
            textView2=(TextView)itemView.findViewById(R.id.textView2);
            textView3=(TextView)itemView.findViewById(R.id.textView3);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
        }
    }
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    public void setmOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }


}
